package com.framework.modules.share;

import com.framework.entity.sys.SysDeptJumpUrl;
import com.framework.entity.sys.SysShare;
import com.framework.entity.sys.SysShareImg;
import com.framework.exception.RRException;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.sys.controller.AbstractController;
import com.framework.page.PageUtils;
import com.framework.service.SysDeptJumpUrlService;
import com.framework.service.agent.SysShareImgServcie;
import com.framework.service.agent.SysShareService;
import com.framework.utils.DateUtils;
import org.jsoup.helper.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.security.provider.SHA;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * 文案控制器
 *
 * @author wq
 */
@RestController
@RequestMapping("sys/share")
public class ShareController extends AbstractController {


    @Autowired
    private SysShareService sysShareService;

    @Autowired
    private SysShareImgServcie sysShareImgServcie;

    @Autowired
    private SysDeptJumpUrlService sysDeptJumpUrlService;

    @RequestMapping("list")
    public RespResult list(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        PageUtils page = sysShareService.queryPage(params);
        return RespResult.ok().put("page", page);
    }

    @RequestMapping("imgList")
    private RespResult imgList(@RequestParam Map<String, Object> params) {
       PageUtils page=sysShareImgServcie.queryImgPage(params);
       return RespResult.ok().put("page",page);
    }

    @RequestMapping("getById/{id}")
    public RespResult getById(@PathVariable Integer id){
        SysShare sysShare = sysShareService.selectById(id);
        return RespResult.ok().put("share",sysShare);

    }

    /**
     * 根据id修改
     * @param sysShare
     * @return
     */
    @RequestMapping("updateById")
    public RespResult updateById(@RequestBody SysShare sysShare){
        sysShareService.updateById(sysShare);
        return RespResult.ok().put("sysShare",sysShare);

    }

    /**
     * 添加分享
     * @param sysShare
     * @return
     */
    @RequestMapping("addShare")
    public RespResult addShare(@RequestBody SysShare sysShare){

        Date shareDate = DateUtils.stringToDate(DateUtils.format(sysShare.getShareDate(), DateUtils.DATE_PATTERN), DateUtils.DATE_PATTERN);
        sysShare.setShareDate(shareDate);
        sysShare.setDeptId(getDeptId());
        sysShare.setCreateTime(new Date());
        Integer shareId=sysShareService.addShare(sysShare);
        return RespResult.ok().put("shareId",shareId);
    }


    @RequestMapping("addImgList")
    public RespResult addImgList(@RequestParam("files") MultipartFile []files,@RequestParam("shareId") Integer shareId) throws IOException {
        Long size;
        Long limitSize=500*1024L;
        System.out.println(shareId);
        SysDeptJumpUrl sysDeptJumpUrl = sysDeptJumpUrlService.queryByDeptIdAndType(getDeptId(),3);
        String prefix=sysDeptJumpUrl.getUploadUrl();
        String suffix;
        String imgUrl;
        int count=1;
        for (MultipartFile file : files) {
            if(file==null){

                throw new RRException("9000","文件不能为空");
            }

            size = file.getSize();

            if(size.compareTo(limitSize)==1){


                throw new RRException("9000","文件大小不能超过500k");
            }
            suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            imgUrl = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);
            SysShareImg sysShareImg=new SysShareImg();
            sysShareImg.setCreateTime(new Date());
            sysShareImg.setImgUrl(imgUrl);
            sysShareImg.setShareId(shareId);
            sysShareImg.setSort(count);
            count++;
            sysShareImgServcie.insert(sysShareImg);
        }

        return RespResult.ok();
    }
}
